package com.jinke.uang.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinke.uang.interfaces.ICommonModule;
import com.jinke.uang.interfaces.ICommonView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class BaseFragment<P extends BasePersenter,M> extends Fragment implements ICommonView {

    public P mPresenter;
    public M mModule;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(getContentResId(), null);
        onInitViews(rootView);
        mPresenter = getPresenter();
        mModule=getModule();
        if(mPresenter!=null && mModule!=null) mPresenter.attach((ICommonModule)mModule,this);
        return rootView;
    }

    protected abstract void onInitViews(View rootView);

    protected abstract int getContentResId();
    protected abstract P getPresenter();
    protected abstract M getModule();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onUiReady();
    }

    protected abstract void onUiReady();



    protected void showLoadingView() {
        if (getActivity() == null || !(getActivity() instanceof BaseActivity)) return;
        ((BaseActivity) getActivity()).showLoadingView();
    }

    protected void dismissLoadingView() {
        if (getActivity() == null || !(getActivity() instanceof BaseActivity)) return;
        ((BaseActivity) getActivity()).dismissLoadingView();
    }
}
